package org.squabble.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.squabble.service.AccountService;
import org.squabble.web.admin.NewsEditCommand;


@Component(value="newsEditValidator")
public class NewsEditValidator implements Validator {

	@Autowired
	AccountService accountService;

	@SuppressWarnings("unchecked")
	@Override
	public boolean supports(Class clazz) {
		return NewsEditCommand.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		NewsEditCommand nec = (NewsEditCommand) arg0;
		if (accountService.getAccount(nec.getUser()) == null) {
			errors.rejectValue("user", "NewsEditCommand.user[not.found]");
		}
	}

}
